<template>
  <el-container class="profile-container">
    <el-header>
      <div class="header-content">
        <h2>个人中心</h2>
        <el-button @click="router.push('/home')">
          <el-icon><Back /></el-icon>
          返回首页
        </el-button>
      </div>
    </el-header>

    <el-main>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-card class="user-info-card">
            <div class="user-avatar">
              <el-avatar :size="100" :src="userInfo.avatar">
                <el-icon><User /></el-icon>
              </el-avatar>
              <h3>{{ userInfo.name }}</h3>
              <p>{{ userInfo.department }}</p>
            </div>
            <el-divider />
            <div class="user-stats">
              <div class="stat-item">
                <h4>工号</h4>
                <p>{{ userInfo.employeeId }}</p>
              </div>
              <div class="stat-item">
                <h4>职位</h4>
                <p>{{ userInfo.position }}</p>
              </div>
            </div>
          </el-card>
        </el-col>

        <el-col :span="16">
          <el-card>
            <template #header>
              <div class="card-header">
                <span>个人信息</span>
                <el-button type="primary" @click="handleEdit">编辑信息</el-button>
              </div>
            </template>

            <el-form :model="userInfo" label-width="100px">
              <el-form-item label="姓名">
                <el-input v-model="userInfo.name" :disabled="!isEditing" />
              </el-form-item>
              <el-form-item label="手机号码">
                <el-input v-model="userInfo.phone" :disabled="!isEditing" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="userInfo.email" :disabled="!isEditing" />
              </el-form-item>
              <el-form-item label="部门">
                <el-input v-model="userInfo.department" :disabled="!isEditing" />
              </el-form-item>
              <el-form-item label="职位">
                <el-input v-model="userInfo.position" :disabled="!isEditing" />
              </el-form-item>

              <el-form-item v-if="isEditing">
                <el-button type="primary" @click="handleSave">保存</el-button>
                <el-button @click="handleCancel">取消</el-button>
              </el-form-item>
            </el-form>
          </el-card>

          <el-card class="mt-20">
            <template #header>
              <div class="card-header">
                <span>安全设置</span>
              </div>
            </template>

            <el-form label-width="100px">
              <el-form-item label="修改密码">
                <el-button @click="handleChangePassword">修改密码</el-button>
              </el-form-item>
            </el-form>
          </el-card>
        </el-col>
      </el-row>
    </el-main>
  </el-container>
</template>

<script lang="ts" setup>
  import { ref, reactive } from 'vue'
  import { useRouter } from 'vue-router'
  import { ElMessage } from 'element-plus'
  import { Back, User } from '@element-plus/icons-vue'

  const router = useRouter()
  const isEditing = ref(false)
  const originalUserInfo = {
    name: '张三',
    employeeId: 'EMP001',
    department: '技术部',
    position: '高级工程师',
    phone: '13800138000',
    email: 'zhangsan@example.com',
    avatar: '',
  }

  const userInfo = reactive({ ...originalUserInfo })

  const handleEdit = () => {
    isEditing.value = true
  }

  const handleSave = () => {
    // TODO: 调用API保存用户信息
    ElMessage.success('保存成功')
    isEditing.value = false
  }

  const handleCancel = () => {
    Object.assign(userInfo, originalUserInfo)
    isEditing.value = false
  }

  const handleChangePassword = () => {
    // TODO: 实现修改密码功能
    ElMessage.info('修改密码功能开发中')
  }
</script>

<style lang="scss" scoped>
  .profile-container {
    min-height: 100vh;

    .header-content {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 100%;
    }

    .user-info-card {
      .user-avatar {
        text-align: center;
        padding: 20px 0;

        h3 {
          margin: 10px 0 5px;
        }

        p {
          color: #666;
          margin: 0;
        }
      }

      .user-stats {
        .stat-item {
          text-align: center;
          padding: 10px 0;

          h4 {
            margin: 0;
            color: #666;
            font-size: 14px;
          }

          p {
            margin: 5px 0 0;
            font-size: 16px;
          }
        }
      }
    }

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    .mt-20 {
      margin-top: 20px;
    }
  }
</style>
